package com.dayu.web.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.dayu.finecomm.pojo.DeviceConfig;
import com.dayu.finecomm.pojo.DevicePusher;
import com.dayu.utils.PropertiesUtil;
import com.dayu.web.param.BaseConfigParam;
import com.dayu.web.utils.YamlUtil;
import com.dayu.web.vo.Destination;

/**
 * 基础配置
 * 
 * @author Chenjiawei
 *
 *         2017年11月14日
 */
@Service
public class ConfigService {

	/**
	 * 获取基础配置信息
	 */
	public BaseConfigParam getBaseConfigs() {

		BaseConfigParam param = new BaseConfigParam();
		param.setServerIp(PropertiesUtil
				.getProperty("service.finecomm.server.tcp.host"));
		param.setServerPort(Integer.parseInt(PropertiesUtil
				.getProperty("service.finecomm.server.tcp.port")));

		List<DeviceConfig> list = YamlUtil.getDeviceConfig();
		Set<String> set = new HashSet<String>();

		for (DeviceConfig dc : list) {
			for (String s : dc.protocols) {
				set.add(s);
			}
		}
		param.setProtocols(set);

		List<Destination> ld = new ArrayList<Destination>();
		List<DevicePusher> pushers = list.get(0).pushers;

		for (DevicePusher push : pushers) {

			Destination dest = new Destination();
			dest.setIp(push.host);
			dest.setPort(push.port);
			ld.add(dest);
		}
		param.setDest(ld);

		return param;
	}

	/**
	 * 修改基础配置
	 */
	public List<DeviceConfig> updateBaseConfigs(List<Destination> dests,
			List<String> protocols) {

		List<DeviceConfig> list = YamlUtil.getDeviceConfig();
		List<DevicePusher> ll = new ArrayList<DevicePusher>();

		for (Destination d : dests) {
			DevicePusher pusher = new DevicePusher();
			pusher.host = d.getIp();
			pusher.port = d.getPort();
			pusher.protocols = protocols;

			ll.add(pusher);
		}

		for (DeviceConfig dc : list) {

			dc.pushers = ll;
		}

		return list;

	}
}
